﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BAL
{
    public partial class SEOutStock : Form
    {
        private LoginService.BarcodeJudgeServices bjs = new BAL.LoginService.BarcodeJudgeServices();
        private string ID;
        public string key = "";
        public string FBillNo = "";
        public int num = 0;
        public string Fry = "";
        public string FSourceBillNo = "";
        public string FSku = "";
        public string Fdate = "";
        public string StickLen = "";
        public string PkgsIsRegular = "";
      
        private decimal total = 0;
        private string NAME = "";
        
        public SEOutStock()
        {
            InitializeComponent();
        }

        public SEOutStock(string _ID)
        {
            InitializeComponent();
            this.ID = _ID;
        }

        private void SEOutStock_Load(object sender, EventArgs e)
        {
            DataGridStyle();
            BinData();         
        }
      
        //获取数据
        private void BinData() 
        {
            DataSet ds = bjs.GetSEOutStockList();
            DataTable dt = new DataTable();

            dt = ds.Tables[0];
            //if (dt.Rows.Count <= 0)
            //{
            //    return;
            //}
            //else
            //{
                dataGrid1.DataSource = dt;
            //}
        }      
        //数据窗口样式
        public void DataGridStyle()
        {
            DataGridTableStyle dts = new DataGridTableStyle();
            dataGrid1.TableStyles.Clear();
            dts.MappingName = "SEOutStock";

            DataGridTextBoxColumn cl1 = new DataGridTextBoxColumn();
            cl1.HeaderText = "序号";
            cl1.MappingName = "No";
            cl1.Width = 20;
            dts.GridColumnStyles.Add(cl1);

            DataGridTextBoxColumn cl2 = new DataGridTextBoxColumn();
            cl2.HeaderText = "出库单号";
            cl2.MappingName = "FBillNo";
            cl2.Width = 130;
            dts.GridColumnStyles.Add(cl2);

            DataGridTextBoxColumn cl3 = new DataGridTextBoxColumn();
            cl3.HeaderText = "开单时间";
            cl3.MappingName = "Fdate";
            cl3.Width = 80;
            dts.GridColumnStyles.Add(cl3);

            DataGridTextBoxColumn cl4 = new DataGridTextBoxColumn();
            cl4.HeaderText = "开单人";
            cl4.MappingName = "NAME";
            cl4.Width = 50;
            dts.GridColumnStyles.Add(cl4);

            DataGridTextBoxColumn cl5 = new DataGridTextBoxColumn();
            cl5.HeaderText = "总米数";
            cl5.MappingName = "total";
            cl5.Width = 40;
            dts.GridColumnStyles.Add(cl5);

            DataGridTextBoxColumn cl6 = new DataGridTextBoxColumn();
            cl6.HeaderText = "总个数";
            cl6.MappingName = "num";
            cl6.Width = 40;
            dts.GridColumnStyles.Add(cl6);

            DataGridTextBoxColumn cl112 = new DataGridTextBoxColumn();
            cl112.HeaderText = "客户名称";
            cl112.MappingName = "Fname";
            cl112.Width = 150;
            dts.GridColumnStyles.Add(cl112);

            dataGrid1.TableStyles.Add(dts);
 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataGridStyle();
            BinData(); 
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Main main = new Main(this.ID);
            main.Show();
            this.Hide();
        }

       
        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
           
            DataGrid dataGrid1 = sender as DataGrid;
            int index = ((DataGrid)sender).CurrentCell.RowNumber;
            if (((DataTable)(dataGrid1.DataSource)).Rows.Count > 0)
            {
                ((DataGrid)sender).Select(index);
            }
             FBillNo = ((DataTable)(dataGrid1.DataSource)).Rows[index]["FBillNo"].ToString();
            NAME = ((DataTable)(dataGrid1.DataSource)).Rows[index]["NAME"].ToString();
            Fry = ((DataTable)(dataGrid1.DataSource)).Rows[index]["Fry"].ToString();
            Fdate = ((DataTable)(dataGrid1.DataSource)).Rows[index]["Fdate"].ToString();
            total = Convert.ToDecimal(((DataTable)(dataGrid1.DataSource)).Rows[index]["total"].ToString());
            num = Convert.ToInt32(((DataTable)(dataGrid1.DataSource)).Rows[index]["num"].ToString());
        
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (FBillNo == null || FBillNo == "")
            {
                MessageBox.Show("请选择要完成的出库单！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
            }
            else 
            {
                //查询有扫描的信息
                int num = bjs.SelScanQty(FBillNo);
                if (num > 0)
                {
                    MessageBox.Show("有未扫描的信息,不能点击完成订单！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    return;
                }
                DialogResult dr = MessageBox.Show("是否完成出库单： " + FBillNo + " ？", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);

                if (dr == DialogResult.Yes)
                {
                    string txt = "";
                    //bool isjd = bjs.jdSEOoutStock(FBillNo, this.ID, out  txt);
                    //if (isjd == false)
                    //{

                    //    MessageBox.Show("同步金蝶数据失败，请联系开发人检测！ " + txt, "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    //    return;
                    //}
                    //else
                    //{
                        bool lb = bjs.FinishSEOoutStock(FBillNo, this.ID, out  txt);
                        if (lb)
                        {
                            MessageBox.Show("完成出库单！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                            DataGridStyle();
                            BinData();
                        }
                        else
                        {
                            MessageBox.Show("完成出库单错误！ " + txt, "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                            return;
                        }
                   // }

                }
                else 
                {
                    return;
                }

            }
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            if (FBillNo == null || FBillNo == "")
            {
                MessageBox.Show("请选择出库单！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            int  number =0;
            SEOutStockDetail icse = new SEOutStockDetail(FBillNo, num, Fry, Fdate, this.ID,out  number);
            if(number==0)
            {
             MessageBox.Show("该单据的条码已经全部提交，请选择完成订单！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                this.Show();
                return;
            }
            else
            {
             icse.Show();
            this.Hide();
            }
           
        }


    }
}